package com.example.lenovo.demo;

import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

public class WebViewActivity extends AppCompatActivity {
    private String url;
    private ImageView imageView;
    private com.tencent.smtt.sdk.WebView tcWebview;
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_web_view);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


        initView();
        initData();
    }

    private void initData() {
        url=getIntent().getStringExtra("url");
        tcWebview.loadUrl(url);
        WebSettings webSettings = tcWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        tcWebview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String s) {
                webView.loadUrl(url);
                return true;
            }
        });
        tcWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int i) {
                super.onProgressChanged(webView, i);

            }
        });


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && tcWebview.canGoBack()) {
            tcWebview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initView() {
        imageView= (ImageView) findViewById(R.id.webviewactivity_imgback);
        tcWebview= (com.tencent.smtt.sdk.WebView) findViewById(R.id.webviewactivity_webview);
        toolbar= (Toolbar) findViewById(R.id.webviewactivity_toolbar);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(tcWebview!=null){
            tcWebview.destroy();
        }
    }
}
